<script setup>
import { findTree } from '@/api/system/organization'
import { queryRoleDetail } from '@/api/system/role.js'

const props = defineProps({
  id: {
    type: String,
    default: ''
  },
  visible: {
    type: Boolean,
    default: false
  }
})
const emit = defineEmits(['update:visible', 'create', 'update'])
const options = [
  {
    value: '1',
    label: '全部数据权限'
  },
  {
    value: '2',
    label: '自定数据权限'
  },
  {
    value: '3',
    label: '本组织数据权限'
  },
  {
    value: '4',
    label: '本组织及以下数据权限'
  },
  {
    value: '5',
    label: '仅本人数据权限'
  }
]

const formRef = ref()
const treeRef = ref()
const pageData = reactive({
  treeData: []
})
const formData = reactive({
  name: '',
  permissionChar: '',
})
const formRules = reactive({
  name: [{ required: true, trigger: 'blur', message: '请输入角色名称！' }],
  permissionChar: [{ required: true, trigger: 'blur', message: '请输入角色权限字符！' }],
})
const queryTreeData = async (val) => {
  if (val === '2') {
    const { data } = await findTree({ name: '' })
    pageData.treeData = data
  }
}
const handleCreate = () => {
  formRef.value?.validate((valid) => {
    if (valid) {
      submitBtnLoading.value = true
      let param = {
        ...formData,
        name: formData.name,
        permissionChar: formData.permissionChar,
      }
      if (!props.id) {
        emit('create', param)
      } else {
        param._id = formData._id
        emit('update', param)
      }
    } else {
      return false
    }
  })
}
const resetForm = () => {
  formRef.value?.resetFields()
  emit('update:visible')
  delete(formData._id)
}
const submitBtnLoading = ref(false)
const editLoading = ref(false)
watch(
  () => props.visible,
  async (newValue, oldValue) => {
    if (newValue) {
      submitBtnLoading.value = false
      const { id } = props
      if (id) {
        editLoading.value = true
        const { data } = await queryRoleDetail(id)
        Object.assign(formData, data)
        if (data.dataScope === '2') {
          await queryTreeData('2')
          data.dataScopeCustomIds.forEach((val) => {
            nextTick(() => {
              treeRef.value.setChecked(val, true, false)
            })
          })
        }
        editLoading.value = false
      }
    }
  }
)
defineExpose({
  submitBtnLoading
})
</script>

<template>
  <el-dialog
    :close-on-click-modal="false"
    v-model="visible"
    :title="id ? '修改角色' : '新增角色'"
    @close="resetForm"
    width="60%"
    :align-center="true"
  >
    <el-form v-loading="editLoading" ref="formRef" :model="formData" :rules="formRules" label-width="80px">
      <div class="px-4">
        <el-form-item prop="name" label="角色名称">
          <el-input v-model="formData.name" placeholder="请输入角色名称"  maxlength="30" />
        </el-form-item>
        <el-form-item prop="permissionChar" label="权限字符">
          <el-input v-model="formData.permissionChar" placeholder="请输入权限字符"  maxlength="50" />
        </el-form-item>
      </div>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" :loading="submitBtnLoading" @click="handleCreate">确认</el-button>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped>
:deep(.el-tree) {
  width: 100%;
}
.el-scrollbar {
  width: 100%;
}
</style>
